package com.ouwen.smartpay.utils;

import com.fm.common.constants.ServiceNameConstant;
import com.google.common.collect.Maps;
import com.ouwen.smartpay.domain.ServiceOrderResultWarpper;

import java.util.Map;
import java.util.Optional;

/**
 * @author <a href="http://youngitman.tech">青年IT男</a>
 * @version v1.0.0
 * @className ServiceOrderMapToServiceResult
 * @description 服务调用方Order与服务调用方返回结果Result映射关系
 * @JunitTest: {@link  }
 * @date 2020-05-06 13:44
 **/
public class ServiceOrderResultMapUtils {

    /*** 调用方请求Order和响应Result与服务码关系 */
    private final static Map<String, ServiceOrderResultWarpper> serviceOrderResultWarpperMap = Maps.newHashMap();

    static {

        //统一支付信息流接口 TODO
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.PAY, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.PAY)
//                .serviceOrderClass(PayingOrder.class).serviceResultClass(PayingResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.CREATE_PAY, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.CREATE_PAY)
//                .serviceOrderClass(CreateAndPayOrder.class).serviceResultClass(CreateAndPayResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.SIGN, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.SIGN)
//                .serviceOrderClass(SignOrder.class).serviceResultClass(SignResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.CONTRACT_PAY, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.CONTRACT_PAY)
//                .serviceOrderClass(ContractPayOrder.class).serviceResultClass(ContractPayOrder.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.DEDUCT, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.DEDUCT)
//                .serviceOrderClass(DeductOrder.class).serviceResultClass(DeductResult.class).build());
//
//        //关于商户类信息流接口
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.REGISTER, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.REGISTER)
//                .serviceOrderClass(MerchRegisterOrder.class).serviceResultClass(MerchRegisterResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.MERCHE_QUERY, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.MERCHE_QUERY)
//                .serviceOrderClass(MerchQueryOrder.class).serviceResultClass(MerchQueryResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.MERCH_UPDATE, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.MERCH_UPDATE)
//                .serviceOrderClass(MerchUpdateOrder.class).serviceResultClass(MerchUpdateResult.class).build());
//
//        //交易订单信息流相关接口
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.CREATE_ORDER, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.CREATE_ORDER)
//                .serviceOrderClass(CreateTradeOrder.class).serviceResultClass(CreateTradeResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.ORDER_QUERY, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.ORDER_QUERY)
//                .serviceOrderClass(QueryTradeOrder.class).serviceResultClass(QueryTradeResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.ORDER_CLOSE, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.ORDER_CLOSE)
//                .serviceOrderClass(CloseingTradeOrder.class).serviceResultClass(CloseingTradeResult.class).build());
//        serviceOrderResultWarpperMap.put(ServiceNameConstant.REBACK, ServiceOrderResultWarpper.builder().servieName(ServiceNameConstant.REBACK)
//                .serviceOrderClass(RebackTradeOrder.class).serviceResultClass(RebackTradeResult.class).build());

    }

    /***
     *
     * 生成key
     *
     * @author liyong
     * @date 5:13 PM 2020/6/12
     * @param serviceName 服务码
     * @param version 版本
     * @exception
     * @return java.lang.String
     **/
    private static String buildKey(String serviceName, String version) {

        return serviceName+":"+version;

    }

    /***
     *
     * 通过服务调用方Order的Class获取服务码
     *
     * @author liyong
     * @date 2:51 PM 2020/5/6
     * @param orderClass
     * @exception
     * @return String
     **/
    public static Optional<String> findServiceNameByOrderClass(Class<?> orderClass) {

        return serviceOrderResultWarpperMap.entrySet().parallelStream()
                .map(Map.Entry::getValue)
                .filter(warpper -> warpper.getServiceOrderClass() == orderClass)
                .map(warpper -> warpper.getServieName()).findFirst();

    }

    /***
     *
     * 通过服务码获取服务调用方Order和Result包装器
     *
     * @author liyong
     * @date 2:51 PM 2020/5/6
     * @param serviceName
     * @exception
     * @return ServiceOrderResultWarpper
     **/
    public static ServiceOrderResultWarpper findByServiceName(String serviceName) {

        return serviceOrderResultWarpperMap.get(serviceName);

    }

}
